"""
Get the question data from the question bank.
Acquisition strategy:
1. Randomly select several questions
2. Adjust the weight extraction of the extracted
   questions according to the historical data (todo)

The returned data is a dict, key is the category,
and value is the questions list. For example:
{
    "操作系统": ["死锁的概念？如何避免死锁？",
                "讲一讲进程间通信的方式"]
    "数据结构与算法": ["写一下快速排序",
                    "写一下堆排序"]
}
"""
import json
import random
from helper.common import logger

class Question_Fetcher:
    def __init__(self, question_file):
        with open(question_file, 'r', encoding='utf-8') as f:
            self.__questions = json.load(f)

    @property
    def questions(self):
        return self.__questions

    def get_random_questions(self, num_per_category=1):
        selected_questions = {}
        for category, questions in self.__questions.items():
            num_questions = min(num_per_category, len(questions))
            selected = random.sample(questions, num_questions)
            selected_questions[category] = selected

        logger.info("Your questions today:\n" + 
                    json.dumps(selected_questions, indent=4, ensure_ascii=False))
        return selected_questions
